#include "Timer1.h"
#include "DIM230.h"
#include <util/delay.h>

void Timer1_init()
{
	SET_BIT(DDRB, 5);
	TCCR1A |= (1<<COM1A1) | (1<<COM1A0) | (1<<WGM11) | (0<<WGM10); // clear OC1A at compare match and 9-bit fast pwm
	TCCR1B |= (0<<WGM13)| (1<<WGM12) | (0<<CS12) | (0<<CS11) | (1<<CS10);			//Prescaler 1 -> 1/16MHz = 62,5ns, 62,5ns
}

uint16_t readTCNT1(void)
{
	unsigned char sreg;
	unsigned int i;
	/* Save global interrupt flag */
	sreg = SREG;
	/* Disable interrupts */
	cli();
	/* Read TCNT1 into i */
	i = TCNT1;
	/* Restore global interrupt flag */
	SREG = sreg;
	return i;

}

void setTCNT1(uint16_t v)
{
	unsigned char sreg;
	/* Save global interrupt flag */
	sreg = SREG;
	/* Disable interrupts */
	cli();
	/* Set TCNT1 to i */
	TCNT1 = v;
	/* Restore global interrupt flag */
	SREG = sreg;

}

void setTCCR1A(uint16_t v)
{
	unsigned char sreg;
	/* Save global interrupt flag */
	sreg = SREG;
	/* Disable interrupts */
	cli();
	/* Set TCNT1 to i */
	OCR1A = v;
	/* Restore global interrupt flag */
	SREG = sreg;

}

void setTCCR1B(uint16_t v)
{
	unsigned char sreg;
	/* Save global interrupt flag */
	sreg = SREG;
	/* Disable interrupts */
	cli();
	/* Set TCNT1 to i */
	TCNT1 = v;
	/* Restore global interrupt flag */
	SREG = sreg;

}

void setTCCR1C(uint16_t v)
{
	unsigned char sreg;
	/* Save global interrupt flag */
	sreg = SREG;
	/* Disable interrupts */
	cli();
	/* Set TCNT1 to i */
	TCNT1 = v;
	/* Restore global interrupt flag */
	SREG = sreg;

}
